package wotd.web;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import wotd.VerseLookup;
import wotd.VerseLookup.VerseSpooler;

import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
@SuppressWarnings("serial")
public class AllVersesServlet extends HttpServlet {
	private static final Logger LOG = Logger.getLogger(AllVersesServlet.class.getName());
	
	/** the excerpt finder */
	@Inject VerseLookup lookup;
	
	@Override
	public void doGet(HttpServletRequest req, final HttpServletResponse resp) throws IOException {
		resp.setContentType("text/html");
		resp.getWriter().print("<link rel=\"stylesheet\" href=\"verse.css\" type=\"text/css\">");

		lookup.getAllVerses(new VerseSpooler() {
			@Override
			public void spool(String next) {
				try {
					resp.getWriter().print(next);
				} catch (IOException e) {
					LOG.log(Level.WARNING, "Error writing excerpt", e);
				}
			}
		});

		resp.getWriter().flush();
	}
}
